## To enable editing in your model, you must also implement setData(),
## and reimplement flags() to ensure that ItemIsEditable is returned.
## You can also reimplement headerData() and setHeaderData() to control
## the way the headers for your model are presented.
#######################################################################
## http://doc.trolltech.com/4.2/qabstractitemmodel.html#subclassing

## Required:flags(),data(),headerData(),rowCount(), columnCount()
## Editable:flags(),setData(),setHeaderData()
## Reizable:insertRows( beginInsertRows() | endInsertRows() )
##          removeRows( beginRemoveRows() | endRemoveRows() )
##          insertColumns( beginInsertColumns() | endInsertColumns() )
##          removeColumns( beginRemoveColumns() | endRemoveColumns() )
## Generally, these fun's should return true if the op. was successful.
## layoutChanged()**
## If finding children; expensive; reimp rowCount() only when ness
## Add & hasChildren(), view will use when possiable
## index() parent()?
## Sorting(Use .sortRol to specify which items are unsortable?)
## QAbstractItemModel::itemData (Custom data rolls?)
## Flags: Qt::ItemIsEnabled Qt::ItemIsSelectable
## qDeleteAll(childItems); <-- Deconsturtor

import pdb
from PyQt4.QtCore import (QAbstractItemModel,Qt,QModelIndex,QVariant)
from PyQt4 import QtGui


class KanjiModel(QAbstractItemModel):
    '''QAbstractItemModel'''

    def __init__(self, parent=None):
        super(KanjiModel, self).__init__(parent)

        ## Inatlize item variables
        ############################
        self.KeydKanji = KeydKanji(self)
        self.MasterPrim = None
        self.KanjiKeyPrim = None
        self.KanjiPrim = None
        self.CompPrim = None
        self.GhostItem = None
        self.counter = 4
        self.counter2 = 4

    ##Pants: Canidate to move to ProxyModel
    def columnCount(self, QModelIndex):
        '''
        Input:  Valid/Invalid QModelIndex
        Output: int
        '''
        if QModelIndex.isValid():
            ##Pants: Temp hack, hard coded values
            return 1
        else:
            return 1
        

    ##Pants: Canidate to move to ProxyModel
    def rowCount(self, QModelIndex):
        '''
        Input:  Valid/Invalid QModelIndex
        Output: int
        '''
        if QModelIndex.isValid():
            print 1
            return 1
        else:
            #pdb.set_trace()
            print len(self.KeydKanji.kkey.keys())
            return len(self.KeydKanji.kkey.keys())

            
    def data(self, QModelIndex, role):
        '''
        Input:  (QModelIndex, Role(s))
        Output: QVariant
        '''
        if role == Qt.DisplayRole:
            #pdb.set_trace()
            self.KeydKanji.kkey[100][QModelIndex]
        else:
            return QVariant()

    ##Pants: Canidate to move to ProxyModel
    def flags(self, QModelIndex):
        '''
        Input:  QModelIndex
        Output: Qt.ItemFlags
        '''
        return Qt.ItemIsEnabled|Qt.ItemIsSelectable

    ##Pants: Canidate to move to ProxyModel
    def headerData(self, section, orientation, Role=Qt.DisplayRole):
        '''
        Input:  section, Qt.Orientation, Role
        Output: Valid/Invalid QVariant
        '''
        z = None
        return z
    
    
    def index(self, row, column, parent=QModelIndex(),new=False):
        '''
        Input:  row, column, Valid/Invalid QModelIndex Parent
        Output: Valid QModelIndex
        '''
        ## Not new, not a valid index
        #pdb.set_trace()
        if new == False and not self.hasIndex(row, column, parent):
            return QModelIndex()

        ## Not new, is a valid index
        elif new == False and self.hasIndex(row, column, parent):
            return self.createIndex(row, column, parent)

        ## New, invalid or valid
        elif new == True:
            return self.createIndex(row, column, parent)
        

    def parent(self, index):
        '''
        Input:  parent item
        Output: Valid/Invalid QModelIndex
        '''
        return QModelIndex()
    def makeItem(self, itemtype, itemdata):
        ## Make sure the class exsists
        if str(eval(itemtype)) == \
           "<class '__main__.{0}'>".format(str(eval(itemtype))):
            ## We have a match, use it
            eval(itemtype).addItem(itemdata)
        else:
            raise ValueError("Buoo Buooooo")
            

        
class KeydKanji(object):
    '''This is where the KanjiModel stores items'''

    def __init__(self, model):
        ## Do some stuff
        self.model = model
        self.kkey = {}
        self.keymap = {}

        
    def loadCsv(self,data):
        rownum = 0
        for x in data:
            
            tempdic = {}
            ##Pants: This is broken...
            self.kkey[x[0]] = tempdic

            tempdic['unichar'] = x[1]
            tempdic['keyword'] = x[2]
            tempdic['isplublic'] = x[3]
            tempdic['mdate'] = x[4][0]
            tempdic['mtime'] = x[4][1]
            tempdic['story'] = x[5]
            tempdic['strprims'] = None
            tempdic['softlinks'] = None
            tempdic['weakref'] = None
            pdb.set_trace()
            tempdic['index'] = self.model.index(rownum,0,new=True)
            print tempdic['index']
            self.keymap[tempdic['index']] = x[0]

        




##import csvProcessor
##data = csvProcessor.pData('temp.csv')
##model = KanjiModel()
##model.KeydKanji.loadCsv(data)
##model = KanjiModel()
##








    
